home *** CD-ROM | disk | FTP | other *** search
- Subject: Re:Using Counted Ptr objects and Collections?
- Sent: 7/15/96 8:32 AM
- Received: 7/15/96 8:39 AM
- From: Greg Friedman, friedman@cognosis.com
- Reply-To: ODF Interest, ODF-Interest@CILabs.ORG
- To: OpenDoc Development Framework Discussion List, ODF-Interest@CILabs.
-
- Itrat Khan wrote:
-
- > I'm using an object that I wish to be both reference-counted (pointer
- > based using FW_TCountedPtr) and part of an ordered collection
- > (FW_TOrderedCollection). Here are the problems I run into (pretend my
- > object's type is called CMyClass):
-
- There is precedent for this sort of thing in ODF. Take a look at
- FW_CScriptableCollection in the Semantic Events subsystem of the Framework
- layer. FW_CScriptableCollection collects instances of FW_MScriptable.
- FW_MScriptable is can optionally be reference counted. FW_MScriptables are
- acquired and released through calls to FW_MScriptable::AcquireScriptable
- and FW_MScriptable::ReleaseScriptable respectively. By default, these
- methods do nothing, since FW_MScriptable will often be mixed into classes
- that are not reference counted. FW_CPropertyDesignator is an example of a
- subclass of FW_MScriptable that implements these methods to provide
- reference counted behavior.
-
- This approach requires consideration of the aspect of reference counted
- objects that suggests that, by definition, reference counted objects are
- not "owned" by other objects - they own themselves. What are the
- implications of adding these object to collections, then? Shouldn't it be
- true, then, that if an object is added to a collection, the collection
- should acquire the object? Shouldn't it be true that the collection should
- release the object when it's removed from the collection? What if the
- collection is destroyed? FW_CScriptableCollection implements these
- behaviors appropriately. It also provides only limited access to collection
- operators, since I wanted to impose some specific design constraints.
-
- I suggest you carefully consider these issues before you decide to go ahead
- with the pattern you are considering. There is nothing wrong with it, but
- you might want to consider all of the implications before deciding to go
- ahead with it.
-
- > * FW_TOrderedCollection methods such as AddFirst() require me to
- > provide a pointer to an object such as (CMyClass* element).
- >
- > * The overloaded -> operator for FW_TCountedPtr won't work with a
- > pointer to an object. The object has to be declared as (CMyClass
- > element).
-
- Remember that reference counting is comprised of two components: a
- representation object that owns a reference count, and a pointer class that
- knows how to incremement and decrement the reference count of
- representation objects (usually via calls to representation methods such as
- Acquire and Release).
-
- I think you may be confusing the two. You should mix FW_MCountedPtrRep into
- CMyClass. When you create your collection, make it a collection of
- CMyClass. You'll then create a second class, possible an instantiation of
- FW_TCountedPtr, that points at objects of CMyClass.
-
- class CMyClass : public FW_MCountedPtrRep
- {
- // words...words...words
- };
-
- typedef FW_TCountedPtr<CMyClass> CMyClassPtr;
-
-
- > So I'd have to do something like this:
- >
- > // ----- Add some arbitrary element to the front of the list. -----
- > CMyClass* element = new CMyClass();
- > orderedCollection->AddFirst(element);
-
-
- The code above becomes:
-
- CMyClassPtr ptr = new CMyClass;
- orderedCollection->AddFirst(ptr);
-
- This works, because of the conversion operator in CMyClassPtr that converts
- ptr to a CMyClass*. As I mentioned earlier, you'll probably want the
- collection to acquire the class object when its added to the collection.
-
- > // ----- Later, retrieve the element and work with it. -----
- > element = orderedCollection->First();
- > (*element)->GetInfoOrSomething(); // This is what gets messy.
-
- becomes:
-
- CMyClassPtr ptr = orderedCollection->First();
- ptr->GetInfoOrSomething(); // not messy at all
-
-
- > I know the first scenario works and I guess the second will too, I'm
- > just wondering if this is the only way I can use these subsystems
- > together or if there's a simpler way. I know this is a C++ question,
- > but can I overload the -> operator so it would work as follows:
- >
- > CMyClass* element;
- > element->GetInfoOrSomething();
- >
- > and behind the scenes it really translates the last line to:
- >
- > element->fRep->GetInfoOrSomething();
-
- The "member selection operator" is already overloaded in the FW_TCountedPtr
- template to do this.
-
- Greg.
-
-
- _________________________________________________________
- Greg Friedman ODF Engineering Apple Computer
-
-
-